﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Xml.Serialization;

namespace SpLocalizer.Common
{
    /// <summary>
    /// Base class for translation project entntnies
    /// </summary>
    public  class ProjectEntity
    {

        /// <summary>
        /// Dicitionary for additional Content properties
        /// </summary>
        private SerializableDictionary<string, string> Properties = null;

        /// <summary>
        /// Return value of custom content property as a string
        /// </summary>
        /// <param name="PropertyName"></param>
        /// <returns></returns>
        protected string GetPropertyValue(string PropertyName)
        {
            if (this.Properties != null)
            {
                return this.Properties[PropertyName];
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// Set value of custom content property as a string
        /// </summary>
        /// <param name="PropertyName"></param>
        /// <returns></returns>
        protected void SetPropertyValue(string PropertyName, string PropertyValue)
        {
            if (this.Properties == null)
            {
                this.Properties = new SerializableDictionary<string, string>();
            }

            this.Properties[PropertyName] = PropertyValue;

        }


        /// <summary>
        /// serialization xml string for saving/restoring custom properties state
        /// </summary>
        public string PropertiesSerializationXml
        {
            get
            {
                if (this.Properties == null || this.Properties.Values.Count == 0)
                    return null;

                XmlSerializer x = new XmlSerializer(typeof(SerializableDictionary<string, string>));
                StringBuilder outputData = new StringBuilder();
                using (XmlWriter writer = XmlWriter.Create(outputData))
                    x.Serialize(writer, this.Properties);

                return outputData.ToString();
            }
            set
            {
                if (String.IsNullOrEmpty(value))
                    return;

                XmlSerializer x = new XmlSerializer(typeof(SerializableDictionary<string, string>));
                using (XmlReader reader = XmlTextReader.Create(new StringReader(value)))
                    this.Properties = x.Deserialize(reader) as SerializableDictionary<string, string>;

            }
        }
    }
}
